package com.example.solrdemo;

import com.example.solrdemo.dao.ARep;
import com.example.solrdemo.dao.HighlightMapper;
import com.example.solrdemo.dao.JsonRequestWriter;
import com.example.solrdemo.pojo.A;
import com.example.solrdemo.pojo.B;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.data.solr.core.query.Field;
import org.springframework.data.solr.core.query.SimpleField;
import org.springframework.data.solr.core.query.result.HighlightEntry;
import org.springframework.data.solr.core.query.result.HighlightPage;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SpringBootApplication
public class SolrDemoApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(SolrDemoApplication.class, args);


        ARep aRep = context.getBean(ARep.class);


        aRep.deleteAll();

        A a = new A();

        a.setContent("今天天气很不错");

        List<B> bs = new ArrayList<>();

        B b = new B();

        b.setValue("哈");


        a.setB(b);

        aRep.save(a);


        List<String> cs = new ArrayList<>();

        cs.add("哈");

        HighlightPage<A> page = aRep.findByBIn(cs, new QPageRequest(0, 5));


        HighlightMapper mapper = context.getBean(HighlightMapper.class);

        mapper.map(page);


        for (A av : page) {
            System.out.println(av.getB().getValue());

        }


        /*--------- 简单操作 ---------*/

        /*//增
        List<SolrInputDocument> docs = new ArrayList<>();
        SolrInputDocument doc = new SolrInputDocument();
        doc.setField("id", "1");
        doc.setField("name", "丁常磊");
        doc.setField("name", "丁磊");
        doc.setField("age", 18);
        docs.add(doc);
        client.add(docs);

        //提交
        client.commit("test");

        //使用id查询
        SolrDocument result = client.getById("1");
        System.out.println(result);
*/
        //使用Id删除
        //client.deleteById("1");

        /*--------- 条件查询 ---------*/
     /*   SolrQuery params = new SolrQuery();

        //查询关键字
        params.set("q", "哈哈");

        //过滤条件
        //params.set("fq", "age:[1 TO 18]");

        //排序
        //params.addSort("age", SolrQuery.ORDER.asc);

        //分页
        params.setStart(0);
        params.setRows(20);

        //默认查询字段
        params.set("df", "b");

        //只查询指定域
        params.set("fl", "id,content,b");

        //高亮
        params.setHighlight(true);
        //指定高亮域
        params.addHighlightField("content");
        //设置前缀
        params.setHighlightSimplePre("<span style='color:red'>");
        //设置后缀
        params.setHighlightSimplePost("</span>");

        QueryResponse queryResponse = client.query("test", params);

        //获取查询结果
        SolrDocumentList results = queryResponse.getResults();

        System.out.println(results.getNumFound());

        //获取高亮结果
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();

        for (SolrDocument r : results) {
            System.out.println(r.get("id"));
            System.out.println(r.get("content"));

            System.out.println(r.get("b"));

            Map<String, List<String>> map = highlighting.get(r.get("id"));

            List<String> list = map.get("name");

//            System.out.println(list.get(0));
        }

*/



    }




}


    /*
        使用:solr create -c test 创建一个core
     */
